﻿<Window x:Class="CSharp2.Capitulo10.Wpf.Produtos.PedidosWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="Pedidos" WindowState="Maximized" MinHeight="600" MinWidth="800"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" FontSize="14" Width="755" Height="578" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.Background>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#FF060606"/>
                <GradientStop Color="#FFBBB6B6" Offset="0.508"/>
                <GradientStop Color="#FF131212" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <GroupBox Header="Pedido" VerticalAlignment="Top" Margin="10,10,10,0" Padding="10" Grid.Row="0" >
            <StackPanel Orientation="Horizontal">
                <Label Content="Vendedor:" VerticalAlignment="Center" Margin="10, 0"/>
                <ComboBox x:Name="vendedorComboBox" Width="200" ItemsSource="{Binding Vendedores}" 
                          DisplayMemberPath="Pessoa.Nome" SelectedValuePath="Id"/>
                <Label Content="CPF do Cliente:" VerticalAlignment="Center" Margin="10, 0"/>
                <TextBox x:Name="cpfCliente" Height="23" TextWrapping="Wrap" Width="100" KeyDown="cpfCliente_KeyDown" />
                <Label x:Name="nomeCliente" Width="200" Margin="10,0" VerticalAlignment="Center" />
            </StackPanel>
        </GroupBox>
        <GroupBox Header="Itens" VerticalAlignment="Top" Margin="10,10,10,0" Padding="10" Grid.Row="1">
            <ScrollViewer x:Name="itensPedidoScrollViewer" VerticalScrollBarVisibility="Auto" MaxHeight="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupBox}}">
                <StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="0, 0, 0, 10">
                        <Label Content="Produto:" VerticalAlignment="Center" Margin="10, 0"/>
                        <ComboBox x:Name="produtosComboBox" Width="150" ItemsSource="{Binding Produtos}" 
                          DisplayMemberPath="Descricao" SelectedValuePath="Id" IsEditable="True"/>
                        <Label DataContext="{Binding ElementName=produtosComboBox, Path=SelectedItem}" Content="{Binding Path=TipoProduto.Descricao}" Width="100" Margin="10,0" 
                           VerticalAlignment="Center" />
                        <Label Content="Qtd:" VerticalAlignment="Center" Margin="10, 0"/>
                        <TextBox x:Name="quantidadeTextBox" Height="23" TextWrapping="Wrap" Width="50" Text="1" TextAlignment="Center"/>
                        <Label Content="Preço:" VerticalAlignment="Center" Margin="10, 0"/>
                        <Label DataContext="{Binding ElementName=produtosComboBox, Path=SelectedItem}" Content="{Binding Path=Custo}" Margin="10,0" 
                           VerticalAlignment="Center" x:Name="custoLabel"/>
                        <Label Content="Total:" VerticalAlignment="Center" Margin="10, 0"/>
                        <TextBox Height="23" TextWrapping="Wrap" IsReadOnly="True" Width="100" >
                            <TextBox.Text>
                                <MultiBinding Converter="{StaticResource MultiplicadorConverter}">
                                    <MultiBinding.Bindings>
                                        <Binding ElementName="custoLabel" Path="Content" />
                                        <Binding ElementName="quantidadeTextBox" Path="Text" />
                                    </MultiBinding.Bindings>
                                </MultiBinding>
                            </TextBox.Text>
                        </TextBox>
                        <Button Content="+" FontSize="20" Width="40" Margin="10, 0" ToolTip="Adicionar" x:Name="adicionarButton" Click="adicionarButton_Click" />
                        <Label x:Name="situacaoPedidoLabel" Width="200" Margin="10,0" VerticalAlignment="Center" Content="Situação">
                            <Label.Visibility>
                                <MultiBinding Converter="{StaticResource VisibilidadePedidoConverter}">
                                    <MultiBinding.Bindings>
                                        <Binding Path="Pedido.Situacao" />
                                        <Binding Path="PodeFecharPedido" />
                                    </MultiBinding.Bindings>
                                </MultiBinding>
                            </Label.Visibility>
                        </Label>
                    </StackPanel>
                    <DataGrid x:Name="itensPedidoDataGrid" ItemsSource="{Binding Path=Pedido.ItensPedido}" AutoGenerateColumns="False" IsReadOnly="True" VerticalAlignment="Center" PreviewMouseWheel="itensPedidoDataGrid_PreviewMouseWheel">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Produto" Binding="{Binding Path=Produto.Descricao}" MinWidth="200" CellStyle="{StaticResource verticalCenterDataGridCell}" />
                            <DataGridTextColumn Header="Tipo" Binding="{Binding Path=Produto.TipoProduto.Descricao}" MinWidth="200"/>
                            <DataGridTextColumn Header="Preço" Binding="{Binding Path=Produto.Custo}" MinWidth="100"/>
                            <DataGridTextColumn Header="Qtd" Binding="{Binding Quantidade}" MinWidth="50"/>
                            <DataGridTextColumn Header="Total" MinWidth="100">
                                <DataGridTextColumn.Binding>
                                    <MultiBinding Converter="{StaticResource MultiplicadorConverter}">
                                        <MultiBinding.Bindings>
                                            <Binding Path="Produto.Custo" />
                                            <Binding Path="Quantidade" />
                                        </MultiBinding.Bindings>
                                    </MultiBinding>
                                </DataGridTextColumn.Binding>
                            </DataGridTextColumn>
                            <DataGridTemplateColumn >
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Content="-" FontSize="20" Width="40" Margin="10, 0" ToolTip="Remover" x:Name="removerButton" Click="removerButton_Click"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </StackPanel>
            </ScrollViewer>
        </GroupBox>
        <GroupBox Header="Total" Margin="10,10,10,0" Padding="10" Grid.Row="2" VerticalAlignment="Top" Height="85">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Label Content="Total:" FontSize="20" VerticalAlignment="Center" Margin="10, 0"/>
                <TextBox FontSize="20" Text="{Binding TotalPedido, Mode=OneWay, StringFormat=\{0:c\}}" IsReadOnly="True" Width="150" HorizontalContentAlignment="Center"/>
                <Button Content="Fechar pedido" FontSize="20" Width="150" Margin="10, 0" x:Name="fecharPedidoButton" Click="fecharPedidoButton_Click" />
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>